/*
 * Copyright (C) 2008 The Android Open Source Project, Romain Guy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package translinkApp.views.table;

import translinkApp.Activities.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TableRow;

/**
 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan because we want
 * to make the bubble taller than the text and TextView's clip is too aggressive.
 */
public class BubbleTableRow extends TableRow {
	private static final float CORNER_RADIUS = 14.0f;

	private final RectF mRect = new RectF();
	private Paint mPaint;
	private Paint mPaintOutline;
	private Drawable mDrawableBottom;
	private int padding = 3;

	public BubbleTableRow(Context context) {
		super(context);
		init();
	}

	public BubbleTableRow(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(getContext().getResources().getColor(R.color.translink_background_light));

		mPaintOutline = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaintOutline.setColor(getContext().getResources().getColor(
				R.color.translink_button_unfocus));

		this.setWillNotDraw(false);
	}

	@Override
	public void onDraw(Canvas canvas) {
		final RectF rect = mRect;
		final int left = getPaddingLeft();
		final int top = getPaddingTop();

		rect.set(left + padding, top + padding, left + getWidth() - padding, top + getHeight()
				- padding);
		RectF rectOutline = new RectF();
		rectOutline.set(left, top, left + getWidth(), top + getHeight());

		canvas.drawRoundRect(rectOutline, CORNER_RADIUS, CORNER_RADIUS, mPaintOutline);
		canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint);

		super.onDraw(canvas);
	}

	@Override
	public void invalidateDrawable(Drawable drawable) {
		if (mDrawableBottom == drawable) {
			final Rect dirty = drawable.getBounds();

			final int drawableWidth = dirty.right - dirty.left;
			final int left = (getWidth() - drawableWidth) / 2 + getScrollX();

			final int drawableHeight = dirty.bottom - dirty.top;
			final int top = getHeight() - getPaddingBottom() - drawableHeight + getScrollY();

			invalidate(left, top, left + drawableWidth, top + drawableHeight);
		}
	}

	@Override
	protected boolean verifyDrawable(Drawable who) {
		return mDrawableBottom == who || super.verifyDrawable(who);
	}
}
